package com.wontube.audio.controller;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.audio.pub.AppConfig;
import com.wontube.audio.pub.AudioCookieUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.extension.utils.net.URLUtils;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.IShortFilmHandler;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.web.i18n.I18nMethod;

/**
 * 首页控制器
 * 
 * @author LuoBiao(2012-4-13)
 * 
 */
@Controller
public class HomeController {
	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping("")
	public ModelAndView dispatchHome(HttpServletRequest request, HttpServletResponse response) {	
		String localeName = AudioCookieUtils.getLocaleName(request);
		//String str = RequestHeadersHolder.holder.<String>getProperty("localeNameMp3");
		return gotoHome(request,  localeName,  response);
	}	
	
	@RequestMapping("/eng")
	public ModelAndView engHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "en_US", response);
	}
	
	@RequestMapping("/jp")
	public ModelAndView jpHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "ja_JP", response);
	}
	
	@RequestMapping("/fr")
	public ModelAndView frHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "fr_FR", response);
	}
	
	@RequestMapping("/zh_tw")
	public ModelAndView twHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "zh_TW", response);
	}
	
	@RequestMapping("/de")
	public ModelAndView deHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "de_DE", response);
	}
	
	@RequestMapping("/it")
	public ModelAndView itHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "it_IT", response);
	}
	
	@RequestMapping("/es")
	public ModelAndView esHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "en_CA", response);
	}
	
	@RequestMapping("/hu")
	public ModelAndView huHome(HttpServletRequest request, HttpServletResponse response) {
		return this.gotoHome(request, "ko_KR", response);
	}

	@RequestMapping("/pa")
	public ModelAndView paHome(HttpServletRequest request,HttpServletResponse response) {
		return this.gotoHome(request, "pa_PK", response);
	}
	
	private ModelAndView gotoHome(HttpServletRequest request, 
			String localeName, HttpServletResponse response) {		
		if(StringUtils.isNullOrEmpty(localeName)){
			localeName = "en_US";
		}
		
		AudioCookieUtils.setLocaleCookie(response, localeName);
		Locale locale = AppConfig.getLocale(localeName);
		
		//RequestHeadersHolder.holder.putProperty("localeNameMp3", localeName);
		ModelAndView mv = getI18nView(locale);
		mv.addObject("locale", localeName);
		
		String youtubeUrl = request.getParameter("url");
		if (youtubeUrl != null && !"".equals(youtubeUrl)) {
			Map<String, String> map = URLUtils.parseQueryString(youtubeUrl);
			String v = map.get("v");
			if (v != null) {
				try {
					response.sendRedirect("convert/" + v);
				} catch (IOException e) {
				}
				return null;
			}
		}
		
		mv.setViewName("index");

		return mv;
	}

	/**
	 * 转化MP3控制器
	 */
	@RequestMapping(value = "/convert/{videoId}")
	public ModelAndView convertMp3(@PathVariable("videoId") String videoId, HttpServletRequest request,
			HttpServletResponse response) {
		//String localeName = RequestHeadersHolder.holder.<String>getProperty("localeNameMp3");

		String localeName = AudioCookieUtils.getLocaleName(request);
		Locale locale = AppConfig.getLocale(localeName);
		ModelAndView mv = this.getI18nView(locale);
		mv.setViewName("index");
		mv.addObject("locale", localeName);

		String userAgent = request.getHeader("User-Agent");
		if (userAgent == null) {
			userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5";
		}

		mv.addObject("userAgent", userAgent);

		Remoting<IShortFilmHandler> remoting = new Remoting<IShortFilmHandler>();
		IShortFilmHandler handler = remoting.createService(IShortFilmHandler.class);
		Movie film = handler.getShortFilm("yt" + videoId);

		if (film == null) {
			try {
				response.sendError(HttpURLConnection.HTTP_NOT_FOUND);
			} catch (IOException e) {
			}
			return null;
		}

		mv.addObject("film", film);

		return mv;
	}

	@RequestMapping(value = "/addon")
	public ModelAndView toAddon(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/addon");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-to-mp3-chrome")
	public ModelAndView toAddonChome(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/youtube-to-mp3-chrome");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-to-mp3-firefox")
	public ModelAndView toAddonFirefox(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/youtube-to-mp3-firefox");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-to-mp3-ie")
	public ModelAndView toAddonIE(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/youtube-to-mp3-ie");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-to-mp3-safari")
	public ModelAndView toAddonSafari(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/youtube-to-mp3-safari");
		return mv;
	}

	@RequestMapping(value = "/about")
	public ModelAndView toAbout(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/about");
		return mv;
	}

	@RequestMapping(value = "/faq")
	public ModelAndView toFAQ(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/faq");
		return mv;
	}

	@RequestMapping(value = "/link")
	public ModelAndView toLink(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/link");
		return mv;
	}
	
	@RequestMapping(value = "/sitemap")
	public ModelAndView toSitemap(HttpServletRequest request) {
		ModelAndView mv = getMVByLocaleName(request);
		mv.setViewName("pub/sitemap");
		return mv;
	}
	
	private ModelAndView getMVByLocaleName(HttpServletRequest request) {
		//String localeName = RequestHeadersHolder.holder.<String>getProperty("localeNameMp3");
		//if (localeName == null || "".equals(localeName)) {
		//	localeName = "en_US";
		//}
		String localeName = AudioCookieUtils.getLocaleName(request);
		Locale locale = AppConfig.getLocale(localeName);
		ModelAndView mv = this.getI18nView(locale);
		mv.addObject("locale", localeName);
		return mv;
	}

	private ModelAndView getI18nView(Locale locale) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("i18n", new I18nMethod(AppConfig.widgetBundleName, locale));
		return mv;
	}
}
